package com.yc.bran.snack.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yc.bran.snack.bean.OrderInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yc.bran.snack.bean.Address;
import com.yc.bran.snack.bean.GoodsTypeSales;
import com.yc.bran.snack.bean.OrderItemInfo;
import com.yc.bran.snack.dto.OrderInfoDTO;
import com.yc.bran.snack.enums.ResultEnmu;
import com.yc.bran.snack.service.ICartInfoService;
import com.yc.bran.snack.service.impl.OrderInfoServiceImpl;
import com.yc.bran.snack.vo.ResultVo;


@RestController
@RequestMapping("/order")
public class OrderInfoController {  

	@Autowired  
	private OrderInfoServiceImpl orderService;

	@Autowired
	private ICartInfoService cartInfoService;

	@Autowired  
	private ObjectMapper objectMapper;

	@Autowired  
	private StringRedisTemplate redisTemplate;

	@PostMapping("/createOrder")  
	public ResultVo createOrder(@RequestBody OrderInfoDTO orderDTO) throws JsonProcessingException {  
	    Boolean isCancelled = orderDTO.getIsCancelled();  
	  
	    if (isCancelled) {  
	        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");  
	        String datePart = dateFormat.format(new Date());  
	        Random random = new Random();  
	        int randomPart = random.nextInt(90000) + 10000;  
	        String ono = datePart + randomPart;  
	  
	        Integer uid = orderDTO.getOrderInfo().getUid();  
	  
	        orderDTO.getOrderInfo().setOno(ono);  
	        String orderJson = objectMapper.writeValueAsString(orderDTO);  
	        System.out.println(orderJson);  
	  
	        redisTemplate.opsForHash().put("cancelled_orders_by_uid:" + uid, ono, orderJson);
	        redisTemplate.expire("cancelled_orders_by_uid:" + uid, 1, TimeUnit.MINUTES);  // 设置整个键的过期时间
	        
	        return new ResultVo(ResultEnmu.SUCCESS);  
	    } else {  
	        System.out.println(isCancelled + "68496496496496");  
	        OrderInfo orderInfo = orderDTO.getOrderInfo();  
	        List<OrderItemInfo> orderItems = orderDTO.getOrderItems();  
	        Address address = orderDTO.getAddress();  
	  
	        orderService.createOrder(orderInfo, orderItems, address);  
	  
	        Integer uid = orderInfo.getUid();  
	  
	        for (OrderItemInfo item : orderItems) {  
	            Integer gid = item.getGid();  
	            cartInfoService.removeFromCart(uid, gid);  
	        }  
	  
	        return new ResultVo(ResultEnmu.SUCCESS);  
	    }  
	}
	
	//	查询待发货商品
	@GetMapping("/selectorder")  
	public ResultVo<List<OrderInfoDTO>> pending(@RequestParam Integer uid) {  
		List<OrderInfoDTO> list = orderService.pendingOrders(uid);  
		if (list == null || list.isEmpty()) {  
			return new ResultVo<>(ResultEnmu.DATA_NULL);  
		}
		return new ResultVo<>(ResultEnmu.SUCCESS, list);  
	}
	
	//	查询已发货商品
	@GetMapping("/selectshippedorder")  
	public ResultVo<List<OrderInfoDTO>> shipped(@RequestParam Integer uid) {  
		List<OrderInfoDTO> list = orderService.shippedOrders(uid);  
		if (list == null || list.isEmpty()) {  
			return new ResultVo<>(ResultEnmu.DATA_NULL);  
		}
		return new ResultVo<>(ResultEnmu.SUCCESS, list);  
	}
	
	// 查询待支付的订单信息
	@GetMapping("/cancelled")  
    public ResultVo<Map<String, Object>> getCancelledOrders(@RequestParam Integer uid) throws IOException {  
        Map<String, Object> orders = orderService.getCancelledOrdersByUid(uid);
        System.out.println(orders);
        return new ResultVo<>(ResultEnmu.SUCCESS, orders);   
    }  
	
	// 查询所有订单信息
	@GetMapping("/selectallorder")
	public ResultVo selectallorder() {
		List list = orderService.selectAllOrder();
		if(list == null) {
			return new ResultVo(ResultEnmu.ERROR);
		}
		return new ResultVo(ResultEnmu.SUCCESS, list);
	}
	
	// 	查询所有待发货商品
	@GetMapping("/selectallshipped")
	public ResultVo selectAllShipped() {
		List list = orderService.selectAllShipped();
		if(list == null) {
			return new ResultVo(ResultEnmu.ERROR);
		}
		return new ResultVo(ResultEnmu.SUCCESS, list);
	}
	
	// 发货
	@GetMapping("/shipments")
	public ResultVo shipments(@RequestParam(required = true) Integer gid) {
		int result = orderService.shipments(gid);
		if(result < 0) {
			return new ResultVo(ResultEnmu.ERROR);
		}
		return new ResultVo(ResultEnmu.SUCCESS);
	}
	
	 
	@GetMapping("/count")
	public ResultVo count() {
		List<GoodsTypeSales> list = orderService.count();
		System.out.println(list);
		if(list == null) {
			return new ResultVo(ResultEnmu.ERROR);
		}
		return new ResultVo(ResultEnmu.SUCCESS, list);
	}
	
	@GetMapping("/monthdata")  
    public List<Map<String, Object>> getMonthlyData() {  
        return orderService.selectMonthlyData();  
    }  
}